package service;


import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;

public class TimeServer extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		return new MyBinder();
	}

	private int countRegis = 0;
	private boolean isStopRegis;
	public int countLogin = 0;
	private boolean isStopLogin;
	private boolean isLoginSucceed = false;// 是否登陆

	public class MyBinder extends Binder {

		public void startRegis() {
			countRegis = 60;
			isStopRegis = false;
			handler.sendEmptyMessage(1);
		}

		public int getCountRegis() {
			return countRegis;
		}

		public void stopRegis() {
			isStopRegis = true;
		}
		// 获取登陆的状态
		public void setLoginSucceed() {
			isLoginSucceed = true;
		}
		
		public boolean getLoginState() {
			return isLoginSucceed;
		}

		public void startLogin() {
			countLogin = 60;
			isStopLogin = false;
			handler.sendEmptyMessage(2);
		}

		public int getCountLogin() {
			return countLogin;
		}

		public void stoLogin() {
			isStopRegis = true;
		}

	}

	@SuppressLint("HandlerLeak") private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				if (isStopRegis) {
					return;
				}
				if (countRegis == 0) {
					return;
				}
				countRegis--;
				handler.sendEmptyMessageDelayed(1, 1000);
			} else {
				if (isStopLogin) {
					return;
				}
				if (countLogin == 0) {
					return;
				}
				countLogin--;
				handler.sendEmptyMessageDelayed(2, 1000);
			}
		}
	};

}
